#ifndef __COMM_HPP_
#define __COMM_HPP_

#include<iostream>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/types.h>
#include<string>
#include"log.hpp"
#include<cstring>
#include<cstdlib>

using namespace std;
//共享内存的大小一般建议是4096的整数倍，内存管理一般是4096byte管理的
const int size=4096;    //4097,实际上给4096*2，但只能访问4097
const string pathname="/home/sbt";
const int proj_id=0x6666;//两份.cc看到的同样的pathname,proj_id
                        //保证key相同
Log log;

key_t GetKey()
{
    key_t k=ftok(pathname.c_str(),proj_id);
    if(k<0)
    {
        log(Fatal,"ftok error: %s",strerror(errno));
        exit(1);
    }
    log(Info,"ftok success,key is:%0x",k);
    return k;
}
//创建共享内存
int GetShareMemHelper(int flag)
{
    key_t k=GetKey();
    int shmid=shmget(k,size,flag);
    if(shmid<0)
    {
        log(Fatal,"create share memory error:%s",strerror(errno));
        exit(2);
    }
    log(Info,"create share memory:success,shmid:%d",shmid);
    return shmid;
}
//创建
int CreateShm()
{
    return GetShareMemHelper(IPC_CREAT|IPC_EXCL|0666);
}
//获取
int GetShm()
{
    return GetShareMemHelper(IPC_CREAT);
}




 #endif